package com.sjz.han.industry.activites;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.sjz.han.industry.MainActivity;
import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;

import java.lang.ref.WeakReference;

public class AppStartActivity extends BaseActivity {
    private class WelcomeHandler extends Handler {
        private final WeakReference<AppStartActivity> reference;

        public WelcomeHandler(AppStartActivity welcomeActivity) {
            reference = new WeakReference<>(welcomeActivity);
        }

        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            final AppStartActivity welcomeActivity = reference.get();
            if (welcomeActivity != null) {
                if (!welcomeActivity.isFinishing()) {
                    switch (msg.what) {
                        case 100://第一次运行
                            //指示界面
                            if(shareUtils.getIsFirstRunning()){
                                if("0".equals(shareUtils.getLevel())){//默认0， 线上支付后为1也就是普通会员
                                   startActivity(new Intent(welcomeActivity, PayActivity.class));
                                }else{
                                    startActivity(new Intent(welcomeActivity, MainActivity.class));
                                }
                            }else{
                                startActivity(new Intent(welcomeActivity, LoginActivity.class));
                            }
                            break;
                        default:
                            break;
                    }
                    welcomeActivity.finish();
                }
            }
        }
    }

    private final WelcomeHandler handler = new WelcomeHandler(this);

    @Override
    protected int setLayoutId() {
        return R.layout.activity_appstart;
    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initViews() {
        //点击home建后 再次点击程序不会走闪频页
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        PermissionsUtil.requestPermission(getApplication(), new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) {
                handler.sendEmptyMessageDelayed(100, 3000);
            }

            @Override
            public void permissionDenied(@NonNull String[] permission) {
                finish();
            }
        }, new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE});


    }

    @Override
    protected void setLisener() {
    }

    @Override
    protected void onRetry() {

    }

}
